package org.kjtc.util;

import java.io.UnsupportedEncodingException;


/**
 * Socket常用数据转换类
 *
 */
public class NetDataTypeTransform {
	public static final String coding="GB2312"; //全局定义，以适应系统其他部分
	public NetDataTypeTransform(){
		
	}
	
	public float byteToFloat(byte[] byt ,int maxlength){
		String tem = ByteArraytoString(byt, maxlength);
		float flo = Float.parseFloat(tem);
		return flo;
	}
	
	/**
	 * 将int转为低字节在前，高字节在后的byte数组(大端)
	 */
	public byte[] IntToByteArrayBig(int n) {
		byte[] b = new byte[4];
		b[0] = (byte) (n & 0xff);
		b[1] = (byte) (n >> 8 & 0xff);
		b[2] = (byte) (n >> 16 & 0xff);
		b[3] = (byte) (n >> 24 & 0xff);
		return b;
	}
	
	/**
	 * 将int转为低字节在前，高字节在后的byte数组(大端)
	 */
	public byte[] IntToTwoBitArray(int n) {
		byte[] b = new byte[2];
		b[0] = (byte) (n & 0xff);
		b[1] = (byte) (n >> 8 & 0xff);
		return b;
	}
	
	
	/**
	 * byte数组转化为int
	 * 将低字节在前转为int，高字节在后的byte数组
	 */
	public int ByteArrayToInt(byte[] bArr) {
         if(bArr.length!=4){
        	 return -1;
         }
		 return (int) ((((bArr[3] & 0xff) << 0)  
	                | ((bArr[2] & 0xff) << 8)  
	                | ((bArr[1] & 0xff) << 16) | ((bArr[0] & 0xff) << 24))); 
	}
	/**
	 * 将byte数组转化成String,为了支持中文，转化时用GBK编码方式
	 */
	public String ByteArraytoString(byte[] valArr,int maxLen) {
		String result=null;
		int index = 0;
		while(index < valArr.length && index < maxLen) {
			if(valArr[index] == 0) {
				break;
			}
			index++;
		}
		byte[] temp = new byte[index];
		System.arraycopy(valArr, 0, temp, 0, index);
		try {
			result= new String(temp,"GBK");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 将String转化为byte,为了支持中文，转化时用GBK编码方式
	 */
	public byte[] StringToByteArray(String str){
		byte[] temp = null;
		if (str==null) {
			return temp;
		}else{
		try {
			temp = str.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {
//			e.printStackTrace();
		}
		}
		return temp;
	}
	
    /**
     * 字节数组转为普通字符串（ASCII对应的字符）
     *
     * @param bytearray
     *            byte[]
     * @return String
     */
    public String bytetoString(byte[] bytearray) {
        String result = "";
        if(bytearray.length < 5) {
            return result;
        }

        int temp = 0;
        for(int n = 0; n < 5; n++) {
            temp = byteConvortInt(bytearray[n]);
            temp = temp >> 4;
            result += (0x0a<=temp && temp <=0x0f)?(char)(temp+87):(char)(temp+48);

            temp=byteConvortInt(bytearray[n]) & 0x0F;
            result += (0x0a<=temp && temp <=0x0f)?(char)(temp+87):(char)(temp+48);
        }
        return result;
    }
	
    public int byteConvortInt(byte b) {
        return b>=0?b:256+b;
    }
    
    // 截取byte数组
    public byte[] subBytes(byte[] src, int begin, int count) {
        byte[] bs = new byte[count];
        for (int i=begin; i<begin+count; i++) {
            bs[i-begin] = src[i];
        }
        return bs;
    }
		
	public  int Ipv4StringToInt(String strIp){
        String[] ss = strIp.split("\\.");
        if(ss.length != 4){
            return 0;
        }
        byte[] bytes = new byte[ss.length];
        for(int i = 0; i < bytes.length; i++){
            bytes[i] = (byte) Integer.parseInt(ss[i]);
        }
        return byte2Int(bytes);
    }
	
	private  int byte2Int(byte[] bytes) {
		
		if (bytes.length != 4) {
			return 0;		
		}
		
        int n = bytes[0] & 0xFF;
        n |= ((bytes[1] << 8) & 0xFF00);
        n |= ((bytes[2] << 16) & 0xFF0000);
        n |= ((bytes[3] << 24) & 0xFF000000);
        return n;
    }
	
	public int byteToIntLittle(byte[] bytes){
		
		if (bytes.length != 4) {
			return 0;		
		}
		
		int n = bytes[0] & 0xFF;
        n |= ((bytes[1] << 8) & 0xFF00);
        n |= ((bytes[2] << 16) & 0xFF0000);
        n |= ((bytes[3] << 24) & 0xFF000000);
        return n;
	}


}
